package com.hakim.wfb.admin.filter;

import com.hakim.wfb.admin.enums.ErrorCodeEnum;
import com.hakim.wfb.bean.Admin;
import com.hakim.wfb.dao.AdminDao;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hkfs.fundamental.validate.ValidateFilter;
import com.hkfs.fundamental.validate.annotaion.ValidateFilterBean;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 */
@ValidateFilterBean
public class AddAdminFilter extends ValidateFilter<AddAdminFilter.AddUserFilterParam> {

    @Autowired
    private AdminDao userDao;

    @Override
    protected void prcoess(AddUserFilterParam param, ResultEx resultEx){
        Admin existsParam = new Admin();
        existsParam.setUserName(param.getUserName());
        int count = userDao.count(existsParam);
        if(count > 0){
            resultEx.makeResult(ErrorCodeEnum.USER_NAME_EXISTS.getCode(), ErrorCodeEnum.USER_NAME_EXISTS.getMessage());
            return;
        }
        resultEx.makeSuccessResult();
    }

    public static class AddUserFilterParam{

        @NotEmpty(message = "用户名不能为空")
        private String userName;

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }
    }
}
